package com.muchfish.reactor.api;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

public class ReactorSchedulersExample {
    public static void main(String[] args) throws InterruptedException {
        Flux.range(1, 1000000)
                .publishOn(Schedulers.boundedElastic())  // 在弹性线程池上发布
                .map(x -> {
                    System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    // 模拟耗时任务
                    for (int i = 0; i < 1000000; i++) {
                        double sqrt = Math.sqrt(i);
                       double j = sqrt++;
                    }
                    return x * x;
                })
                .subscribeOn(Schedulers.parallel())  // 在并行线程池上订阅
                .subscribe(v -> {
                    System.out.println("subscribe = " + Thread.currentThread().getName());
                    System.out.println("v = " + v);
                });


        // 睡一会 等任务执行完
        Thread.sleep(5000);
    }
}

